package net.starwifi.duobao.web;


import net.starwifi.duobao.entity.Feedback;
import net.starwifi.duobao.entity.Member;
import net.starwifi.duobao.mvc.BaseController;
import net.starwifi.duobao.mvc.Message;
import net.starwifi.duobao.service.FeedbackService;
import net.starwifi.duobao.service.MemberService;
import net.starwifi.duobao.utils.HtmlCleanEditor;
import net.starwifi.duobao.utils.times.DateTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;


/**
 * Created by Administrator on 2015/9/28.
 */
@Controller("apiFeedbackController")
@RequestMapping("/feedback")
public class FeedbackController extends BaseController {


    @Autowired
    FeedbackService feedbackService;

    @Autowired
    MemberService memberService;

    /**
     * 新增
     */
    @RequestMapping( method = RequestMethod.GET)
    public String feedback() {
        return "feedback/feedback";
    }

    /**
     * 新增
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(int cat,String content) {
        Member member=memberService.getCurrent();
        Date beginData= DateTool.setHms(new Date(), 0, 0, 0);
        Date endData= DateTool.setHms(new Date(), 23, 59, 59);
        Long count=feedbackService.getQueryCount("o.memberId=?1 and o.createDate>?2 and o.createDate<?3", new Object[]{member.getId(), beginData, endData});
        if(count>=3){
            return Message.error("已超过今日反馈上限,请明天再来");
        }
        if(content==null||content.trim().equals("")||content.trim().length()<15){
            return Message.error("反馈内容不能小于15个汉字");
        }
        String text=HtmlCleanEditor.getClearText(content);
        Feedback feedback=new Feedback();
        feedback.setIsRead(Boolean.FALSE);
        feedback.setContent(text);
        feedback.setMobile(member.getMobile());
        feedback.setCat(cat);
        feedback.setMemberId(member.getId());
        feedbackService.save(feedback);
        return Message.success("您的反馈已经提交成功,谢谢您的意见！");
    }

    /**
     * 新增
     */
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Object create(String content) {
        Member member=memberService.getCurrent();
        Feedback feedback=new Feedback();
        feedback.setIsRead(Boolean.FALSE);
        feedback.setContent(content);
        feedback.setMemberId(member.getId());
        feedbackService.save(feedback);
        return Message.success("成功");
    }

}
